package com.generic.action;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.HibernateException;

import com.generic.ds.HibernateSession;
import com.generic.handler.impl.MemberHandler;
import com.generic.module.impl.Member;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;

@InterceptorRef("jsonValidationWorkflowStack")
public class SignUpBuyer extends ActionSupport {

	private static final long serialVersionUID = 4822689189743604387L;
	private String email = null;
	private String password = null;
	private String password2 = null;
	private String firstName = null;
	private String lastName = null;
	private String gender = null;
	private Date birthday = null;

	public String getEmail() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public String getPassword2() {
		return password2;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getGender() {
		return gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@EmailValidator(key = "error.invalid")
	public void setEmail(String email) {
		this.email = email;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@StringLengthFieldValidator(key = "error.length", trim = true, minLength = "6", maxLength = "20")
	public void setPassword(String password) {
		this.password = password;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@FieldExpressionValidator(key = "error.notequal", expression = "password eq password2")
	public void setPassword2(String password) {
		this.password2 = password;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@StringLengthFieldValidator(key = "error.length", trim = true, minLength = "2", maxLength = "20")
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@StringLengthFieldValidator(key = "error.length", trim = true, minLength = "2", maxLength = "20")
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@RequiredStringValidator(key = "error.required")
	public void setGender(String gender) {
		this.gender = gender;
	}

	@RequiredFieldValidator(key = "error.required", shortCircuit = true)
	@DateRangeFieldValidator(key = "error.daterange", min = "01/01/1900", max = "12/31/2005")
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@SkipValidation
	public String execute() {
		return SUCCESS;
	}

	@Action(value = "/sign-up-buyer-add", results = { @Result(name = "input", location = "ajax-msg.jsp"), @Result(name = "success", location = "ajax-msg.jsp") })
	public String add() throws Exception {
		HibernateSession hs = new HibernateSession();
		try {
			hs.beginTransaction();

			MemberHandler dao = new MemberHandler(hs);
			Member member = (Member)dao.get("email", email);
			if(member == null) {
				dao.add(getMember());
				hs.commitTransaction();
				addActionMessage(getText("success"));
			} else {
				addFieldError("email", getText("error.duplicate"));
			}
		} catch (HibernateException e) {
			hs.rollbackTransaction(e);
		} finally {
			hs.closeSession();
		}

		return SUCCESS;
	}

	private Member getMember() {
		Member member = new Member();
		member.setEmail(email);
		member.setPassword(password);
		member.setFirstName(firstName);
		member.setLastName(lastName);
		member.setGender(gender);
		member.setBirthday(birthday);
		return member;
	}
}